//
//  Human.swift
//  SitPosition
//
//  Created by apple on 2019/2/9.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation
import Upsurge


/**
 * 关于人的骨架定义
 */
class Human {
    
    var pairs : [Connection]
    var bodyParts : [Int: BodyPart]
    var uidxList: Set<String>
    
    init(_ pairs: [Connection]) {
        self.pairs = [Connection]()
        self.bodyParts = [Int: BodyPart]()
        self.uidxList = Set<String>()
        
        for pair in pairs {
            self.addPair(pair)
        }
    }
    
    func _getUidx(_ partIdx: Int,_ idx: Int) -> String {
        return String(format: "%d-%d", partIdx, idx)
    }
    
    func addPair(_ pair: Connection) {
        self.pairs.append(pair)
        
        self.bodyParts[pair.partIdx1] = BodyPart(_getUidx(pair.partIdx1, pair.idx1),
                                                 pair.partIdx1,
                                                 pair.coord1.0,
                                                 pair.coord1.1,
                                                 pair.score)
        self.bodyParts[pair.partIdx2] = BodyPart(_getUidx(pair.partIdx2, pair.idx2),
                                                 pair.partIdx2,
                                                 pair.coord2.0,
                                                 pair.coord2.1,
                                                 pair.score)
        let uidx: [String] = [
            _getUidx(pair.partIdx1, pair.idx1),
            _getUidx(pair.partIdx2, pair.idx2)
        ]
        self.uidxList.formUnion(uidx)
    }
    
    func merge(_ other: Human){
        for pair in other.pairs {
            self.addPair(pair)
        }
    }
    
    func isConnected(_ other: Human) -> Bool {
        return uidxList.intersection(other.uidxList).count > 0
    }
    
    func partCount() -> Int {
        return self.bodyParts.count
    }
    
    func getMaxScore() -> Double {
        return max(self.bodyParts.map{ $0.value.score })
    }
    
}

class BodyPart {
    
    var uidx: String
    var partIdx: Int
    var x: CGFloat
    var y: CGFloat
    var score: Double
    
    init(_ uidx: String,_ partIdx: Int,_ x: CGFloat,_ y: CGFloat,_ score: Double) {
        self.uidx = uidx
        self.partIdx = partIdx
        self.x = x
        self.y = y
        self.score = score
    }
}


struct Connection {
    
    var score: Double
    var idx1: Int
    var idx2: Int
    var partIdx1: Int
    var partIdx2: Int
    var coord1: (CGFloat,CGFloat)
    var coord2: (CGFloat,CGFloat)
    var score1: Double
    var score2: Double
    
    init(score: Double,
         partIdx1: Int,
         partIdx2: Int,
         idx1: Int,
         idx2: Int,
         coord1: (CGFloat,CGFloat),
         coord2: (CGFloat,CGFloat),
         score1: Double,
         score2: Double) {
        
        self.score = score
        self.score1 = score1
        self.score2 = score2
        self.coord1 = coord1
        self.coord2 = coord2
        self.idx1 = idx1
        self.idx2 = idx2
        self.partIdx1 = partIdx1
        self.partIdx2 = partIdx2
    }
}
